﻿//********************************************************************************//
//创建日期：<创建日期，2016-04-11>
//创建作者：<谭小康，tanxiaokang@shanghai3h.com>
//功能说明：水平衡管理
//********************************************************************************//
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Web.Class;

namespace Web.Pages.YongShui.ShuiPingHGL
{
    public partial class ShuiPingHengGL : BasePage
    {
        #region 属性

        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        /// <summary>
        ///当前操作项ID
        /// </summary>
        private int SHUIPINGHGLID
        {
            get
            {
                return Convert.ToInt32(ViewState["SHUIPINGHGLID"]);
            }
            set
            {
                ViewState["SHUIPINGHGLID"] = value;
            }
        }

        #endregion

        #region 变量

        /// <summary>
        /// 水平衡管理实体对象
        /// </summary>
        YS_SPHGL_SHUIPINGHGL _YS_SPHGL_SHUIPINGHGL = new YS_SPHGL_SHUIPINGHGL();

        /// <summary>
        /// 水平衡管理BLL对象
        /// </summary>
        YS_SPHGL_SHUIPINGHGLBLL _YS_SPHGL_SHUIPINGHGLBLL = new YS_SPHGL_SHUIPINGHGLBLL();

        /// <summary>
        /// 数据访问操作类
        /// </summary>
        SpHelper _spHelper = new SpHelper();

        /// <summary>
        /// 用户信息对象
        /// </summary>
        YS_YG_YONGHUXX _YS_YG_YONGHUXX = new YS_YG_YONGHUXX();

        /// <summary>
        /// 用户信息BLL
        /// </summary>
        YS_YG_YONGHUXXBLL _YS_YG_YONGHUXXBLL = new YS_YG_YONGHUXXBLL();

        /// <summary>
        /// DataTable的行集合
        /// </summary>
        DataRowCollection drc;

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                Display();
            }
            else
            {
                this.gvShuiPingHengGL.DataSource=this.dtSource;
                this.gvShuiPingHengGL.DataBind();
            }
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender); 
        }

        /// <summary>
        /// grid添加
        /// </summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //初始化数据
            InitData();
            this.Status = GridViewStatus.Add;
            Display();
        }

        /// <summary>
        /// grid放弃
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Init;            
            Display();
        }

        /// <summary>
        /// grid修改
        /// </summary>
        protected void lnbModify_Command(object sender, CommandEventArgs e)
        {
            this.SHUIPINGHGLID = Convert.ToInt32(e.CommandArgument);
            this.Status = GridViewStatus.Update;
            Display();
            SetData();
        }

        /// <summary>
        /// grid删除
        /// </summary>
        protected void lnbDel_Command(object sender, CommandEventArgs e)
        {
            //获取当前要操作对象的记录ID
            this.SHUIPINGHGLID = Convert.ToInt32(e.CommandArgument);
            //根据主键获取水平衡管理对象
            this._YS_SPHGL_SHUIPINGHGL = _YS_SPHGL_SHUIPINGHGLBLL.SelectYS_SPHGL_SHUIPINGHGL(this.SHUIPINGHGLID,null);
            if (this._YS_SPHGL_SHUIPINGHGL.I_PRINT == 1)
            {
                PageTools.AlertMsg(this, "该条记录已经打印过报表不允许删除！");
                return;
            }
            this._YS_SPHGL_SHUIPINGHGL.S_CAOZUOR = this.UserID.ToString();
            this._YS_SPHGL_SHUIPINGHGL.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            this._YS_SPHGL_SHUIPINGHGL.I_JLZT = -1;
            _SYS_LOGBLL.Log<YS_SPHGL_SHUIPINGHGL>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "水平衡管理", OperateState.Delete, _YS_SPHGL_SHUIPINGHGL, null);
            if (_YS_SPHGL_SHUIPINGHGLBLL.InsertOrUpdate(_YS_SPHGL_SHUIPINGHGL, null))
            {
                
                LoadData();
            }
            PageTools.AlertMsg(this, "操作成功！");           
        }

        /// <summary>
        /// grid保存
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveData();           
        }

        /// <summary>
        /// 查询按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
            InitData();
        }

        /// <summary>
        /// 用户号回车事件
        /// </summary>
        protected void txtuserNoButton_Click(object sender, EventArgs e)
        {
            LoadData1(true);
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            //加载水平衡类型
            WordsManager manger = new WordsManager();
            PageTools.BindComboBox(cmbshuipinghenglx, manger.Instance.FindWordsDataTableByBelongID(WordsStruct.BelongID.水平衡类型));
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            this.txtYongHuH.Text = "";
            this.txtYongHuM.Text = "";
            this.YongHuAddress.Text = "";
            this.txtuserNo.Text = "";
            this.txtuserName.Text = "";
            this.txtAddress.Text = "";
            this.txtzonggaiXMS.Text = "";
            this.txtYiWanChengXMS.Text = "";
            this.txtZongTouZiJE.Text = "";
            this.txtJieShuiLiang.Text = "";
            this.txtDuLouL.Text = "";
            this.txtDuLouXMS.Text = "";
            this.txtDLouTouZiJE.Text = "";
            this.txtGuanJin.Text = "";
            this.txtXinShuiLiang.Text = "";
            this.cmbshuipinghenglx.SelectedIndex = -1;
            this.txtbeianRQ.Text = "";
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            this.dtSource = _spHelper.P_YS_SHUIPINGHENGGL(this.txtYongHuH.Text.Trim() == "" ? null : this.txtYongHuH.Text.Trim(), this.txtYongHuM.Text.Trim() == "" ? null : this.txtYongHuM.Text.Trim(), this.YongHuAddress.Text.Trim() == "" ? null : this.YongHuAddress.Text.Trim()).Tables[0];
            this.gvShuiPingHengGL.DataSource = this.dtSource;
            this.gvShuiPingHengGL.DataBind();
        }

        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    btnAdd.Visible = true;
                    GridViewContentTable.Visible = false;
                    this.gvShuiPingHengGL.Enabled = true;
                    break;
                case GridViewStatus.Search:
                    btnAdd.Visible = true;
                    GridViewContentTable.Visible = true;
                    break;
                case GridViewStatus.Update:
                    btnAdd.Visible = false;
                    GridViewContentTable.Visible = true;
                    this.gvShuiPingHengGL.Enabled = false;

                    //this.cmbshuipinghenglx.Enabled = false;
                    //this.txtbeianRQ.Enabled = false;
                    this.cmbshuipinghenglx.Enabled = true;
                    this.txtbeianRQ.Enabled = true;

                    this.txtuserNo.Enabled = false;
                    this.txtuserName.Enabled = false;
                    this.txtAddress.Enabled = false;
                    this.cbGuoQi.Visible = true;
                    this.cbGuoQi.Enabled = false;
                    this.lblOperateTip.Text = "修改";
                    break;
                case GridViewStatus.Add:
                    btnAdd.Visible = false;
                    GridViewContentTable.Visible = true;
                    this.gvShuiPingHengGL.Enabled = false;
                    this.lblOperateTip.Text = "添加";
                    this.txtuserNo.Enabled = true;
                    this.txtuserName.Enabled = false;
                    this.txtAddress.Enabled = false;
                    this.gvShuiPingHengGL.Enabled = true;
                    this.cmbshuipinghenglx.Enabled = true;
                    this.txtbeianRQ.Enabled = true;
                    this.cbGuoQi.Visible = false;
                    break;
            }
        }

      
        /// <summary>
        /// 给输入框赋值
        /// </summary>
        private void SetData()
        {
            //根据主键获取水平衡管理对象
            _YS_SPHGL_SHUIPINGHGL = _YS_SPHGL_SHUIPINGHGLBLL.SelectYS_SPHGL_SHUIPINGHGL(this.SHUIPINGHGLID, null);
            //获取用户信息对象
            drc=new YS_YG_YONGHUXXBLL().SelectYS_YG_YONGHUXXesDynamic("i_jlzt=0 and i_leixing=0 and s_yonghuh='"+ _YS_SPHGL_SHUIPINGHGL.S_YONGHUH+"'",null).Rows;
            this.txtuserNo.Text = _YS_SPHGL_SHUIPINGHGL.S_YONGHUH;
            this.txtuserName.Text = Convert.ToString(drc[0]["S_YONGHUM"]);
            this.txtAddress.Text = Convert.ToString(drc[0]["S_DIZHI"]);
            this.txtzonggaiXMS.Text =Convert.ToString(_YS_SPHGL_SHUIPINGHGL.I_ZONGGAIXMS);
            this.txtYiWanChengXMS.Text = Convert.ToString(_YS_SPHGL_SHUIPINGHGL.I_YIWANCHENGXMS);
            this.txtZongTouZiJE.Text = Convert.ToString(_YS_SPHGL_SHUIPINGHGL.N_ZONGTOUZIJE);
            this.txtJieShuiLiang.Text = Convert.ToString(_YS_SPHGL_SHUIPINGHGL.N_JIESHUIL);
            this.txtDuLouL.Text =Convert.ToString(_YS_SPHGL_SHUIPINGHGL.N_DULOUL);
            this.txtDuLouXMS.Text =Convert.ToString(_YS_SPHGL_SHUIPINGHGL.N_DULOUXM);
            this.txtDLouTouZiJE.Text = Convert.ToString(_YS_SPHGL_SHUIPINGHGL.N_DULOUTZJE);
            this.txtGuanJin.Text = Convert.ToString(_YS_SPHGL_SHUIPINGHGL.N_GUANJIN);
            this.txtXinShuiLiang.Text = Convert.ToString(_YS_SPHGL_SHUIPINGHGL.N_XINSHUIL);
            this.cmbshuipinghenglx.Value = Convert.ToString(_YS_SPHGL_SHUIPINGHGL.I_SHUIPINGHENGTYPE);
            this.txtbeianRQ.Date =Convert.ToDateTime( _YS_SPHGL_SHUIPINGHGL.D_BEIANRQ);
            this.cbGuoQi.Checked = DateTime.Now > _YS_SPHGL_SHUIPINGHGL.D_BEIANRQ.Value.AddYears(5);
        }

        /// <summary>
        /// 添加水平衡记录时保存数据
        /// </summary>
        private void SaveData()
        {
            OperateState operateState;
            if (this.txtbeianRQ.Text=="")
            {
                PageTools.AlertMsg(this, "请选择备案日期！");
                return;
            }
            DataRowCollection drc = this._YS_SPHGL_SHUIPINGHGLBLL.SelectYS_SPHGL_SHUIPINGHGLsDynamic(string.Format("I_JLZT=0 and S_Yonghuh='{0}' and rownum=1", this.txtuserNo.Text),null).Rows;
            if (drc.Count == 1)
            {
                _YS_SPHGL_SHUIPINGHGL.D_BEIANRQ = Convert.ToDateTime((drc[0]["D_BEIANRQ"]));
            }
            //添加
            if (this.Status == GridViewStatus.Add)
            {
                operateState = OperateState.Add;
                this.SHUIPINGHGLID = 0;
                this._YS_SPHGL_SHUIPINGHGL.I_JLZT = 0;
                this._YS_SPHGL_SHUIPINGHGL.I_PRINT = 0;
                if (drc != null && Convert.ToDateTime(_YS_SPHGL_SHUIPINGHGL.D_BEIANRQ).AddYears(5) > BusinessTool.GetDataBaseDateTime())
                {
                    PageTools.AlertMsg(this, "五年内有记录无法添加新记录！");
                    this.Status = GridViewStatus.Init;
                    this.Display();
                    return;
                }
            }
            //修改
            else
            {
                operateState = OperateState.Modify;
                _YS_SPHGL_SHUIPINGHGL = _YS_SPHGL_SHUIPINGHGLBLL.SelectYS_SPHGL_SHUIPINGHGL(this.SHUIPINGHGLID, null);               
            }           
            _YS_SPHGL_SHUIPINGHGL.S_YONGHUH = this.txtuserNo.Text.Trim();
            if (this.txtzonggaiXMS.Text.Trim() != "")
            {
                _YS_SPHGL_SHUIPINGHGL.I_ZONGGAIXMS = Convert.ToInt32(this.txtzonggaiXMS.Text.Trim());
            }
            if (this.txtYiWanChengXMS.Text.Trim() != "")
            {
                _YS_SPHGL_SHUIPINGHGL.I_YIWANCHENGXMS = Convert.ToInt32(this.txtYiWanChengXMS.Text.Trim());
            }
            if (this.txtZongTouZiJE.Text.Trim() != "")
            {
                _YS_SPHGL_SHUIPINGHGL.N_ZONGTOUZIJE = Convert.ToDecimal(this.txtZongTouZiJE.Text.Trim());
            }
            if (this.txtJieShuiLiang.Text.Trim() != "")
            {
                _YS_SPHGL_SHUIPINGHGL.N_JIESHUIL = Convert.ToDecimal(this.txtJieShuiLiang.Text.Trim());
            }
            if (this.txtDuLouL.Text.Trim() != "")
            {
                _YS_SPHGL_SHUIPINGHGL.N_DULOUL = Convert.ToDecimal(this.txtDuLouL.Text.Trim());
            }
            if (this.txtDuLouXMS.Text.Trim() != "")
            {
                _YS_SPHGL_SHUIPINGHGL.N_DULOUXM = Convert.ToDecimal(this.txtDuLouXMS.Text.Trim());
            }
            if (this.txtDLouTouZiJE.Text.Trim() != "")
            {
                _YS_SPHGL_SHUIPINGHGL.N_DULOUTZJE = Convert.ToDecimal(this.txtDLouTouZiJE.Text.Trim());
            }
            if (this.txtGuanJin.Text.Trim() != "")
            {
                _YS_SPHGL_SHUIPINGHGL.N_GUANJIN = Convert.ToDecimal(this.txtGuanJin.Text.Trim());
            }
            if (this.txtXinShuiLiang.Text.Trim() != "")
            {
                _YS_SPHGL_SHUIPINGHGL.N_XINSHUIL = Convert.ToDecimal(this.txtXinShuiLiang.Text.Trim());
            }
            if (this.cmbshuipinghenglx.Value != null)
            {
                _YS_SPHGL_SHUIPINGHGL.I_SHUIPINGHENGTYPE = Convert.ToInt32(this.cmbshuipinghenglx.Value);
            }
            if (this.txtbeianRQ.Value != null)
            {
                _YS_SPHGL_SHUIPINGHGL.D_BEIANRQ = Convert.ToDateTime(this.txtbeianRQ.Date);
            }
            this._YS_SPHGL_SHUIPINGHGL.S_CAOZUOR = this.UserID.ToString();
            this._YS_SPHGL_SHUIPINGHGL.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            _SYS_LOGBLL.Log<YS_SPHGL_SHUIPINGHGL>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "水平衡管理", operateState, _YS_SPHGL_SHUIPINGHGL, null); 
            if (_YS_SPHGL_SHUIPINGHGLBLL.InsertOrUpdate(_YS_SPHGL_SHUIPINGHGL, null))
            {
                               
                //更新水平衡备案信息
                _spHelper.P_YSYG_UPDATESPHXX(_YS_SPHGL_SHUIPINGHGL.S_YONGHUH);
                PageTools.AlertMsg(this, "保存成功！");
                this.Status = GridViewStatus.Init;
                Display();
                LoadData();
                //定位记录
                this.SHUIPINGHGLID = Web.Class.PageTools.FocusOpreateRecord(this.dtSource, this.gvShuiPingHengGL, "S_YONGHUH", _YS_SPHGL_SHUIPINGHGL.S_YONGHUH, true);
            }
            else
            {
                PageTools.AlertMsg(this, "保存失败！");
            }
        }

        /// <summary>
        /// 查询用户
        /// </summary>
        private void LoadData1(bool alert)
        {
            if (!string.IsNullOrEmpty(this.txtuserNo.Text))
            {
                DataRowCollection drc = this._YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic(string.Format("I_JLZT=0 and S_Yonghuh='{0}' and rownum=1", this.txtuserNo.Text), "I_LEIXING DESC").Rows;
                if (drc.Count == 1)
                {
                    //记录当前用户号
                    this.txtuserNo.Text = (drc[0]["S_YONGHUH"]).ToString();
                    this.txtuserName.Text = drc[0]["S_YONGHUM"].ToString();
                    this.txtAddress.Text = drc[0]["S_DIZHI"].ToString();                  
                }
                else
                {
                    if (alert)
                    {
                        PageTools.AlertMsg(this, "该用户不存在,请重新输入！");
                    }
                }
            }
        }

        #endregion

      
     

    }
}